<?php

class Admin_ChiNhanhController extends Zend_Controller_Action{
	
	public function init()
	{
		$this->_helper->layout->setLayout('admin');
	}
	
	public function indexAction()
	{
		$chichanhMapper = Library_Model_ChiNhanhMapper::getInstance();
		$chinhanh = $chichanhMapper->chinhanhList();
		$this->view->chinhanh = $chinhanh;
	}
	
	public function themAction()
	{
		$this->view->title = 'Thêm chi nhánh';
		$formThem = new Admin_Form_ChiNhanhForm();
		
		$formThem->setMethod('POST');
		$formThem->setAction('/zfMHB/public/admin/chinhanh/them');
						
		$formThem->addElement('submit', 'submit', array('label' => 'Thêm'));
		$formThem->getElement('tenchinhanh')->addValidator('Db_NoRecordExists',true, array(
				'table' => 'tbl_chinhanh',
				'field' => 'TenChinhanh'
		));
		$this->view->form = $formThem;
		
		if($this->getRequest()->isPost())		{
			if($formThem->isValid($_POST))			{
				$data = $formThem->getValues();
				
				$tenchinhanh = $data['tenchinhanh'];
				$diachi = $data['diachi'];
				$dienthoai = $data['dienthoai'];
				
				$chinhanhMapper = Library_Model_ChiNhanhMapper::getInstance();
				$chinhanhObj = new Library_Model_ChiNhanh();
				
				$chinhanhObj->setTenchinhanh($tenchinhanh);
				$chinhanhObj->setDiachi($diachi);
				$chinhanhObj->setDienthoai($dienthoai);
				
				$chinhanh = $chinhanhMapper->save($chinhanhObj);
				$this->_redirect('/admin/chinhanh/index');
			}
		}
	}
	
	public function capnhatAction()
	{
		$this->view->title = 'Cập nhật chi nhánh';
		$formCapnhat = new Admin_Form_ChiNhanhForm();
		
		$formCapnhat->setMethod('POST');
		$formCapnhat->setAction('/zfMHB/public/admin/chinhanh/capnhat');
		
		$formCapnhat->addElement('submit', 'submit', array('label' => 'Lưu'));
		$formCapnhat->addElement('hidden', 'id');
		$chinhanhMapper = Library_Model_ChiNhanhMapper::getInstance();

		$machinhanh = $this->getRequest()->getParam('id');
		$this->view->form = $formCapnhat;
		
		if($this->getRequest()->isPOST())	{
			if($formCapnhat->isValid($_POST))	{
				
				$data = $formCapnhat->getValues();
				$id = $data['id'];
				$tenchinhanh = $data['tenchinhanh'];
				$diachi = $data['diachi'];
				$dienthoai = $data['dienthoai'];
				
				$chinhanhObj = new Library_Model_ChiNhanh();
				
				$chinhanhObj->setId($id);
				$chinhanhObj->setTenchinhanh($tenchinhanh);
				$chinhanhObj->setDiachi($diachi);
				$chinhanhObj->setDienthoai($dienthoai);
				
				$chinhanh = $chinhanhMapper->save($chinhanhObj);
				
				$this->_redirect('/admin/chinhanh/index');
			}
		}
		else
		{
			$chinhanh = $chinhanhMapper->searchChiNhanhById($machinhanh);
			
			$formCapnhat->getElement('id')->setValue($chinhanh->getId());
			$formCapnhat->getElement('tenchinhanh')->setValue($chinhanh->getTenchinhanh());
			$formCapnhat->getElement('diachi')->setValue($chinhanh->getDiachi());
			$formCapnhat->getElement('dienthoai')->setValue($chinhanh->getDienthoai());
			
		}
	}
	
	public function xoaAction()
	{
		$this->_helper->viewRenderer->setNoRender(true);
		$macn = $this->getRequest()->getParam('id');
		$chinhanhMapper = Library_Model_ChiNhanhMapper::getInstance();
		$chinhanhMapper->delete($macn);
		$this->_redirect('/admin/chinhanh/index');
	}
	
}